#include <time.h>

#include <QString>

#include "client.h"
#include "protocol.h"
#include "babel.h"
#include "ui_babel.h"


void CSProtocol::Register::sendTreat(TCPClient &connexion)
{
    CSProtocol::Header header = CSProtocol::Header();
    header.code = CSProtocol::Register::code;
    header.dataLength = sizeof(CSProtocol::Register);
    header.spontaneous = false;


    QString qLogin = dynamic_cast<Babel*>(connexion.parent()->parent())->ui->login->text();
    QString qPassword = dynamic_cast<Babel*>(connexion.parent()->parent())->ui->password->text();
    strcpy(this->login, qLogin.toStdString().c_str());
    strcpy(this->pass, qPassword.toStdString().c_str());
    connexion.writeData(header, this);
}

void CSProtocol::Register::receiveTreat(TCPClient &connexion, IData *data)
{
    Babel * window = dynamic_cast<Babel *>(connexion.parent()->parent());
    CSProtocol::Header *header = connexion.getReadingHeader();
    (void)connexion;
    (void)data;
    if (header->code == CSProtocol::Ok::code)
    {
        qDebug() << "Register ok";
        window->ui->message->setText((static_cast<CSProtocol::Ok*>(data))->what);
        window->ui->message->show();
    }
    else
    {
        window->ui->message->setText((static_cast<CSProtocol::Ko*>(data))->what);
        window->ui->message->show();
        qDebug() << header->code;
        qDebug() << "Register ko";
    }
}

